//
//  ViewController.m
//  TableViewDemo
//
//  Created by jintao he on 2018/12/17.
//  Copyright © 2018 jintao he. All rights reserved.
//

#import "ViewController.h"
#import <ZhuoZhuo/ZhuoZhuo.h>
#import "TestTableViewCell.h"
#import <CommonCrypto/CommonDigest.h>

#define W_SELF __weak __typeof(self) weakSelf = self;
static NSString *const TestTableViewCell_re_id = @"TestTableViewCell";

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *plazaTable;
@property (nonatomic, strong) NSArray<ResponseModel *> *dataAry;
@property (nonatomic, strong) UIActivityIndicatorView * activityView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configVc];
    [self getDataFromLocal];
}

-(void)configVc{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.plazaTable];
    [self configActivityView];
}

-(void)configActivityView{
    self.activityView.center = CGPointMake([[UIScreen mainScreen] bounds].size.width/2.0, [[UIScreen mainScreen] bounds].size.height/2.0);
    [self.view addSubview:self.activityView];
    
}

- (void)closeActivityView{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.activityView stopAnimating];
        self.activityView.hidden = YES;
    });
}

-(void)getDataFromLocal{
    W_SELF
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        RdTestGetResource__NotAllowedInMainThread();
        
        @autoreleasepool {
            weakSelf.dataAry = RdTestGetResource__NotAllowedInMainThread();
            
            for (ResponseModel *model in weakSelf.dataAry) {
                @autoreleasepool {
                    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.ImageUrl]]];
                    
                    NSString *path_document = NSHomeDirectory();
                    
                    NSString *imagePath = imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.png",[self getMd5String:model.ImageUrl]]];
                    
                    [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
                }
            }
        }
        
        [weakSelf closeActivityView];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.plazaTable reloadData];
        });
        
    });
}

-(NSString *)getMd5String:(NSString *)targetStr{
    const char *data = [targetStr UTF8String];
    
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(data, (CC_LONG)strlen(data), digest);
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
        @autoreleasepool {
            [result appendFormat:@"%02x", digest[i]];
        }
    }
    return result;
}

#pragma mark -- delegate && datasource

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ResponseModel *model = _dataAry[indexPath.row];
    return [TestTableViewCell cellHeightForType:model];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataAry.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ResponseModel *model = _dataAry[indexPath.row];
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TestTableViewCell_re_id forIndexPath:indexPath];
    [cell setPostData:model];
    
    cell.highLightTextTapBlock = ^(NSString * _Nonnull str, NSString * _Nonnull url) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"targetStr为%@,url为%@",str,url] preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定"  style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:sureAction];
        
        [self  presentViewController:alert animated:YES completion:nil];
    };
    
    return cell;
}

#pragma mark -- lazy

-(UITableView *)plazaTable{
    if (!_plazaTable) {
        _plazaTable = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _plazaTable.backgroundColor = [UIColor whiteColor];
        _plazaTable.delegate = self;
        _plazaTable.dataSource = self;
        _plazaTable.tableFooterView = [UIView new];
        [_plazaTable registerClass:[TestTableViewCell class] forCellReuseIdentifier:TestTableViewCell_re_id];
    }
    return _plazaTable;
}

-(UIActivityIndicatorView *)activityView{
    if (!_activityView) {
        _activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [_activityView startAnimating];
    }
    return _activityView;
}

@end
